// with polyfills
import 'core-js/stable'
import 'regenerator-runtime/runtime'
// import Antd from 'ant-design-vue';
import { message } from 'ant-design-vue'

import axios from 'axios'
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/'
import i18n from './locales'
import request, { VueAxios } from './utils/request'
import ProLayout, { PageHeaderWrapper } from '@ant-design-vue/pro-layout'
import themePluginConfig from '../config/themePluginConfig'

// mock
// WARNING: `mockjs` NOT SUPPORT `IE` PLEASE DO NOT USE IN `production` ENV.
// import './mock'

import bootstrap from './core/bootstrap'
import './core/lazy_use' // use lazy load components
import './permission' // permission control
import './utils/filter' // global filter
import './global.less'
// 需要单独注册一下
import { FormModel } from 'ant-design-vue' // global style

Vue.config.productionTip = false

// mount axios to `Vue.$http` and `this.$http`
Vue.use(VueAxios)
// Vue.use(Antd,{
//   size:"small"
// })
// use pro-layout components
Vue.component('pro-layout', ProLayout)
Vue.component('page-container', PageHeaderWrapper)
Vue.component('page-header-wrapper', PageHeaderWrapper)
Vue.use(FormModel)

Vue.prototype.$get = function (url, config) {
  if (!url) throw Error('请核对请求链接是否正确')
  console.log('config', config)
  // const token = window.localStorage.storetoken
  const { params, headers, ...otherConfig } = config
  console.log('params', params)
  const header = Object.assign(headers || {}, {
    // 'Content-Type': 'application/json; charset=utf-8',
    // 'Auth-Token': token,
    // 'Common-Param': getCommonParam()
  })
  return axios.get(url, {
    params,
    headers: header,
    ...otherConfig
  }).catch((error) => { message.error(error) })
}
// http://172.25.236.83:8088
Vue.prototype.$post = function (url, params, config = {}, type = 1) {
  if (!url) throw Error('请核对请求链接是否正确')
  // const token = window.localStorage.storetoken
  // const {...otherConfig} = config
  let source
  if (type == 1) {
    source = { 'Content-Type': 'application/json; charset=utf-8' } // json 数据
  } else if (type == 2) {
    source = { 'Content-Type': 'multipart/form-data' } // formData表单
  } else {
    source = { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } // 普通表单
  }
  const header = Object.assign({}, source)
  return axios.post(url, params, {
    headers: header,
    ...config,
    ...source
  }).catch((error) => { message.error(error) })
}

axios.interceptors.request.use(config => {
  console.log('request interceptor')
  // const token = storage.get(ACCESS_TOKEN)
  // 如果 token 存在
  // 让每个请求携带自定义 token 请根据实际情况自行修改
  config.headers.common['Authorization'] = ' Bearer ' + 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJob3N0VGVhbSI6IiIsInNpZHMiOiIiLCJzdWIiOiJKRVRBVVRIIiwiaG9zdFRyaWJlIjoiIiwicmVhbHVzZXJuYW1lIjoi566h55CG5ZGY566h55CG5ZGYIiwiZGV2ZWxvcG1lbnRDZW50ZXIiOiIiLCJpYXQiOjE2NzM1MDQwMjAsInVzZXJuYW1lIjoidGVzdDAxIiwic3RhdHVzIjoiMSJ9.8fh2qFSl6Hp8o2U9AAmbcL-5VW1oNcAqv_sv2H0ut3E'

  return config
})
axios.interceptors.response.use(function (response) {
  console.log('response', response)
  if (response.data.code == 20001) {
    // return Message.error(response.data.msg || "请求出错请稍后重试")
  }
  return response
}, function (error) {
  const handleError = JSON.parse(JSON.stringify(error))
  // console.log(handleError)
  // console.log("出错了",error.status)
  // console.log("出错了",error.message)
  // console.log("出错了",error.error)
  if (handleError.response.data.status != 401) {
    // Message.error(error.message)
  }
  // Message.error(error.message)
  // if (error.response) {
  //   switch (handleError.response.data.status) {
  //
  //     case 401:
  //       router.replace({
  //         path: '/login',
  //         query: { redirect: router.currentRoute.fullPath }
  //       })
  //       Cookies.remove('user')
  //       Cookies.remove('token')
  //       Cookies.remove('refresh_token')
  //       Cookies.remove('realusername')
  //   }
  // }
  return Promise.reject(error)
})
window.umi_plugin_ant_themeVar = themePluginConfig.theme

new Vue({
  router,
  store,
  i18n,
  // init localstorage, vuex, Logo message
  created: bootstrap,
  render: h => h(App)
}).$mount('#app')
